//
//  NBAOfCell.m
//  MY Project For Sport
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "NBAOfCell.h"
#import "NetworkHandle.h"
#import "ModelForNBA.h"
#import "CellForRecommendNews.h"
#import "ModelForRecommend.h"
#import "CellForNBAFlash.h"
#import "CellForRecommedImage.h"
#import "CellForNBAAds.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface NBAOfCell ()<UITableViewDataSource, UITableViewDelegate, NetworkHandleDelegate>


@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation NBAOfCell

-(void)dealloc {
  
    [_array release];
    [super dealloc];

}


- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        
        [self createTableView];
        [self handleData];
        
    }
    
    return self;
    
}

- (void)handleData {

NSString *str = @"http://platform.sina.com.cn/sports_client/feed?app_key=2586208540&_version__=3.2.1.01&__os__=android&feed_id=653&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_show%2Cctime%2Cvid%2Cvideo_info%2Cstitle%2Cwapsummary&num=20&partner=clear&pos=nba&sport_tour=nba&ad=1&pdps_params=%7B%22app%22%3A%7B%22timestamp%22%3A%221453810085109%22%2C%22os%22%3A%22Android%22%2C%22model%22%3A%22ZTE+G717C%22%2C%22device_type%22%3A%224%22%2C%22osv%22%3A%224.2.2%22%2C%22name%22%3A%22cn.com.sina.sports%22%2C%22carrier%22%3A%22%E4%B8%AD%E5%9B%BD%E7%94%B5%E4%BF%A1%22%2C%22device_id%22%3A%22A0000038A97F12%22%2C%22make%22%3A%22ZTE+G717C%22%2C%22channel%22%3A%22%22%2C%22connection_type%22%3A%222%22%2C%22version%22%3A30201001%2C%22ip%22%3A%22fe80%3A%3A4ecb%3Af5ff%3Afe3e%3Ae562%25wlan0%22%7D%7D&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771";
    
    [NetworkHandle handlerJSONWitURL:str delegate:self];

}

- (void)handleDidComplete:(id)result {

    self.array = [NSMutableArray array];
    
    NSDictionary *dic = [result objectForKey:@"result"];
    
    NSDictionary *dict = [dic objectForKey:@"data"];
    
    NSDictionary *dics = [dict objectForKey:@"feed"];
      NSArray *array = [dics objectForKey:@"data"];
    
    for (NSDictionary *dic in array) {
        
        ModelForNBA *model = [[ModelForNBA alloc] init];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.array addObject:model];
        
        [model release];
}
    
    [self.tableView reloadData];
 
    
   // http://platform.sina.com.cn/sports_client/feed?app_key=2586208540&_version__=3.2.1.01&__os__=android&feed_id=653&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_show%2Cctime%2Cvid%2Cvideo_info%2Cstitle%2Cwapsummary&num=20&ctime=1454026345&pos=nba&sport_tour=nba&ad=1&pdps_params=%7B%22app%22%3A%7B%22timestamp%22%3A%221454068518907%22%2C%22os%22%3A%22Android%22%2C%22model%22%3A%22ZTE+G717C%22%2C%22device_type%22%3A%224%22%2C%22osv%22%3A%224.2.2%22%2C%22name%22%3A%22cn.com.sina.sports%22%2C%22carrier%22%3A%22%E4%B8%AD%E5%9B%BD%E7%94%B5%E4%BF%A1%22%2C%22device_id%22%3A%22A0000038A97F12%22%2C%22make%22%3A%22ZTE+G717C%22%2C%22channel%22%3A%22%22%2C%22connection_type%22%3A%222%22%2C%22version%22%3A30201001%2C%22ip%22%3A%22fe80%3A%3A4ecb%3Af5ff%3Afe3e%3Ae562%25wlan0%22%7D%7D&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771
    
    
    
}



/**创建 tabelView */
-(void)createTableView{
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[CellForRecommendNews class] forCellReuseIdentifier:@"poolForNBANews"];
    [self.tableView registerClass:[CellForRecommedImage class] forCellReuseIdentifier:@"poolForNBARecommendImage"];
    
    [self.tableView registerClass:[CellForNBAFlash class] forCellReuseIdentifier:@"poolForNBAFlash"];
    
    [self.tableView registerClass:[CellForNBAAds class] forCellReuseIdentifier:@"poolForNBAAds"];

}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.array.count;

}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    ModelForNBA *model = [self.array objectAtIndex:indexPath.item];
    if (([model.categoryid isEqualToString:@"1"] || [model.categoryid isEqualToString:@"3"])&& model.wapsummary != nil) {
        ModelForRecommend *model2 = [self.array objectAtIndex:indexPath.item];
        CellForRecommendNews *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForNBANews"];
        
        [cell passModelForRecommend:model2];
        
        return cell;
    }else if ([model.categoryid isEqualToString:@"partner"]){
        CellForNBAFlash *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForNBAFlash"];
        
        [cell passModelForNBA:model];
        
        return cell;
        
    }else if ([model.categoryid isEqualToString:@"2"]){
    
        ModelForRecommend *model2 = [self.array objectAtIndex:indexPath.item];
        CellForRecommedImage *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForNBARecommendImage"];
        
        [cell passModelForRecommedImage:model2];
        
        return cell;
        
    } else {
        
        CellForNBAAds *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForNBAAds"];
        
        [cell passModelForNBAAds:model];
        
        return cell;
     }
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"disappear" object:nil];

    if ([self.delegate respondsToSelector:@selector(didClickNBARow:)]) {
        
        [self.delegate didClickNBARow:[self.array objectAtIndex:indexPath.row]];
        
    }
 
}

-(void)layoutSubviews {

    [super layoutSubviews];

    self.tableView.frame = CGRectMake(0, self.contentView.frame.size.height / 15 , self.contentView.frame.size.width, self.contentView.frame.size.height - self.contentView.frame.size.height / 15);

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ModelForRecommend *model = [self.array objectAtIndex:indexPath.item];
    
    if (([model.categoryid isEqualToString:@"1"] || [model.categoryid isEqualToString:@"3"])&& model.wapsummary != nil) {
        
        return self.contentView.frame.size.height / 5.5;
        
    } else if ([model.categoryid isEqualToString:@"2"] || [model.categoryid isEqualToString:@"partner"]) {
        
        return self.contentView.frame.size.height / 4.5;
        
    } else {
        
        return 80 * HEIGHTT;
        
    }
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
